package com.example.wechatlistview.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.wechatlistview.tools.CustomTools;
import com.example.wechatlistview.controls.SimperListAdapter;
import com.example.wechatlistview.resmanager.DataSource;
import com.example.wechatlistview.R;

public class fg_contacts extends Fragment {

    Context mContext;

    ListView listView = null;
    ListView preListView = null;
    private boolean isAdapterDeployed = false;
    private SimpleAdapter preAdapter;
    private SimpleAdapter adapter;

    /**
     * 定义功能头像资源与名字数组
     */
    private Object[] preImageId;
    private String[] preName;

    /**
     * 定义联系人头像资源与名字数组
     */
    private Object[] imageId;
    private String[] name;
    private String[] key;
    private int[] ctrlId;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_contacts, container, false);

        preListView = (ListView) v.findViewById(R.id.contactFuncList);
        listView = (ListView) v.findViewById(R.id.contactList);

        key = new String[]{"headimg", "headtitle"};

        preImageId = new Object[]{
                R.mipmap.heads_contacts_newfriend, R.mipmap.heads_contacts_chatsonly, R.mipmap.heads_contacts_gropchat,
                R.mipmap.heads_contacts_tag, R.mipmap.heads_contacts_official, R.mipmap.heads_contacts_cpcontacts
        };
        imageId = new Object[]{
                R.mipmap.heads_contacts_1, R.mipmap.heads_contacts_2, R.mipmap.heads_contacts_3, R.mipmap.heads_contacts_4,
                R.mipmap.heads_contacts_5, R.mipmap.heads_contacts_6, R.mipmap.heads_contacts_7, R.mipmap.heads_contacts_8, R.mipmap.heads_contacts_9
        };

        ctrlId = new int[]{R.id.headimg, R.id.headtitle};

        return v;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = getActivity();
        DataSource dataSource = new DataSource(mContext);
        preName = dataSource.GetStrArrayFromRes(R.array.contacts_preName);
        name = dataSource.GetStrArrayFromRes(R.array.contacts_name);

        if (listView != null) {

            /**
             * 自定义万能适配器类SimperListAdapter实现
             * ->添加搜索栏头布局
             * ->调用万能适配器
             * ->适配器传入列表视图控件
             * ->重新测量列表高度
             **/
            View searchView = View.inflate(mContext, R.layout.headview_chats, null);
            preListView.addHeaderView(searchView);
            if (!isAdapterDeployed) {
                SimperListAdapter preSimperListAdapter = new SimperListAdapter(mContext, R.layout.listview_layout, key, new Object[][]{preImageId, preName}, ctrlId);
                preAdapter = preSimperListAdapter.GetAdapter();

                SimperListAdapter simperListAdapter = new SimperListAdapter(mContext, R.layout.listview_layout, key, new Object[][]{imageId, name}, ctrlId);
                adapter = simperListAdapter.GetAdapter();
                isAdapterDeployed = true;
            }

            preListView.setAdapter(preAdapter);
            listView.setAdapter(adapter);

            //重新测量列表总高度
            CustomTools.setListViewHeightBasedOnChildren(preListView);
            CustomTools.setListViewHeightBasedOnChildren(listView);

        }
    }


}
